package com.zhiyou100.crm.servlet.customerCategory;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.zhiyou100.crm.model.CustomerCategory;
//import com.zhiyou100.crm.service.CustomerCategoryService;
//import com.zhiyou100.crm.service.impl.CustomerCategoryServiceImpl;
import com.zhiyou100.crm.service.CustomerCategoryService;
import com.zhiyou100.crm.service.impl.CustomerCategoryServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.CustomerCategory;
import com.zhiyou100.crm.util.User;

/**
 * 更新分类
 */
@WebServlet("/customerCategory/update")
public class UpdateServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	CustomerCategoryService customerCategoryService = new CustomerCategoryServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		int customerCategoryId = Integer.parseInt(request.getParameter("id"));
		List<CustomerCategory> customerCategorys = customerCategoryService.queryAll();
		for (int i = 0; i < customerCategorys.size(); i++) {
			if(customerCategorys.get(i).getCustomer_category_id()==customerCategoryId){
				request.setAttribute("customerCategory", customerCategorys.get(i));
				break;
			}
		}
		request.getRequestDispatcher("/WEB-INF/view/customerCategory/update.jsp").forward(request,response);
//		response.sendRedirect(request.getContextPath()+"/customerCategory/list");
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String customerCategoryName = request.getParameter("customerCategoryName");
		String customerCategoryDesc = request.getParameter("customerCategoryDesc");
		int customerCategoryId = Integer.parseInt(request.getParameter("customerCategoryId"));
		User user = (User) request.getSession().getAttribute("user");
		SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long l = System.currentTimeMillis();
		String format =  df.format(new Date(l));
		Timestamp time= Timestamp.valueOf(format);
		//更新时间
		CustomerCategory category=new CustomerCategory(customerCategoryId,customerCategoryName,customerCategoryDesc,time,user.getUser_id());
		int update = customerCategoryService.update(category);
		if(update>0) {
			response.sendRedirect(request.getContextPath() + "/customerCategory/list");
		}else {
			response.sendRedirect(request.getHeader("referer"));
		}
	}

}
